/**
 *   类说明
 * 
 *   @creator         xldc	497937995@qq.com  
 *   @create-time     2014年1月11日   下午6:47:33   
 */
package com.huacheng.cms.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huacheng.cms.dao.UserMapper;
import com.huacheng.cms.exception.BaseException;
import com.huacheng.cms.model.User;
import com.huacheng.cms.util.AuthUtils;

@Service
public class UserService {
	
	@Autowired
	private UserMapper userM;

	public boolean addOne(User user) throws BaseException {
		int selects = -1;
		User u = new User();
		u.setName(user.getName());
		selects = userM.selectBySelectiveCount(u);
		if (selects == 0) {
			u.setPassword(AuthUtils.getPassword(u.getName(),user.getPassword()));
			u.setCreatetime(new Date());
			userM.insertSelective(u);
		} else {
			return false;
		}
		return true;
	}

	public User getOneByKey(long userid) {
		return userM.selectByPrimaryKey(userid);
	}

	public int deleteOneByKey(long userid) {
		return userM.deleteByPrimaryKey(userid);
	}

	public int updateOneByObj(User user) throws BaseException {
		if (!user.getPassword().equals("")) {
			user.setPassword(AuthUtils.getPassword(user.getPassword(), user.getName()));
		}
		user.setLasttime(new Date());
		return userM.updateByPrimaryKeySelective(user);
	}

	public int getListCountByObj(User user) {
		return userM.selectBySelectiveCount(user);
	}

	public List<User> getListByObj(User user, long offset, long rows) {
		return userM.selectBySelective(user, offset, rows);
	}
}
